<?php

	/*
	 * The SMS main page
	 * Handles SMS messages (editing, delete)
	 * Handles filters (phonenumbers and contents)
	 *
	 * Variables:
	 *
	 * Returns: HTML page
	 *
	 */


	$script = '
	function addfiltword(){
		var temp = prompt("Please enter filter word");
		if(temp != undefined){
			$.get("'.$url_prefix.'ajax/filterwordsave/&txt="+temp, function(data){
				$("#fwords").append(data);
				$("#fwords").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});
			});
		}
	}
	function delfiltword(id){
		if(confirm("Are you sure you want to remove this word from the filter?")){
			$.get("'.$url_prefix.'ajax/filterworddelete/&id="+id, function(data){
				$("#filtword"+id).fadeOut();
				$("#fwords").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});
			});
		}
	}
	function addfiltnr(){
		var temp = prompt("Please enter cellphone number to ban", arguments[0]);
		if(temp != undefined){
			$.get("'.$url_prefix.'ajax/filternumbersave/&txt="+temp, function(data){
				if(data == "ok"){
					$("#fnumbers").append(data);
					$("#fnumbers").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});
				}
			});
		}
	}
	function delfiltnr(id){
		if(confirm("Are you sure you want to allow this number again?")){
			$.get("'.$url_prefix.'ajax/filternumberdelete/&id="+id, function(data){
				$("#filtnr"+id).fadeOut();
				$("#fnumbers").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});
			});
		}
	}

	function addsms(){
		editsms(0);
	}
	function editsms(id){
		$.get("'.$url_prefix.'ajax/editsms/&id="+id, function(data){
			$(".editbox").html(data);
			$(".editbox").fadeIn("fast");
		});
	}
	function editsmsclose(){
		$(".editbox").fadeOut("fast");
	}
	function editsmssave(){
		var tid = $(".editbox #id").val();
		var ttxt = $(".editbox #editbox_textarea").val();
		$.post("'.$url_prefix.'ajax/savesms/", { id: tid, txt: ttxt }, function(data){
			if(tid > 0){
				$("#tdsms"+tid).html(data);
			}
			// New sms will get loaded on next auto-reload...
			editsmsclose();
		});
	}
	function delwarn(id){
		var temp = $("#tdsms"+id).text();
		if(confirm("Are you sure you want to remove the SMS \'"+temp+"\'?")){
			$.get("'.$url_prefix.'ajax/deletesms/", { id: id }, function(data){
				if(data == "ok"){
					$("#tdsms"+id).parent().slideUp();
				}else{
					alert(data);
				}
			});
		}
	}

	var SMSmaxID = 0;
	var SMSminID = 9999999;

	function loadsmslist(prevID, direc, maxnr){
		$("#smsloadlistindicator").css("display","inline");

		request = $.ajax({
			url: "'.$url_prefix.'ajax/loadsmslist/&dir="+direc+"&lim="+maxnr+"&id="+prevID,
			processData: false,
			success: function(xml){
				$(request.responseXML).find("sms").each(function(){
					$("#topbar").after("<tr><td>"+$(this).attr("time")+"</td><td id=\"tdsms"+$(this).attr("id")+"\">"+$(this).text()+"</td><td style=\"text-align:right\"><a class=\"fb\" href=\"javascript:editsms("+$(this).attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_edit.png\" /></a><a class=\"fb\" href=\"javascript:addfiltnr("+$(this).attr("from")+")\"><img src=\"'.$url_prefix.'images/page_white_error.png\" /></a><a class=\"fb\" href=\"javascript:delwarn("+$(this).attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_delete.png\" /></a></td></tr>");
					SMSmaxID = Math.max(SMSmaxID, $(this).attr("id"));
					if($(this).attr("id") > 0){
						SMSminID = Math.min(SMSminID, $(this).attr("id"));
					}
				});

				if($(request.responseXML).find("sms").length > 0){
//					$("#smsinnerlist").jScrollPane({scrollbarWidth:20, scrollbarMargin:10});
					$("#smsinnerlist").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});
				}

				$(".smslist tr").hover(
					function(){$(this).addClass("stripelight");},
					function(){$(this).removeClass("stripelight");}
				);
				$("#smsloadlistindicator").css("display","none");
				setTimeout("loadsmslist("+SMSmaxID+",\"up\", 100)", 2000);

			}
		});

	}


		function loadbannedsmslist(){
			$("#smsloadlistindicator").css("display","inline");
			request = $.ajax({
				url: "'.$url_prefix.'ajax/loadbannedsmslist/",
				processData: false,
				success: function(xml){
					// Delete current messages
					var curlen = $("#bannedSMS tr").length;
					$("#bannedSMS tr").each(function(i){
						if((i == 0) || (i >= (curlen-1))){ ; }else{
							$(this).remove();
						}
					});

					$(request.responseXML).find("sms").each(function(){
						$("#bantopbar").after("<tr><td>"+$(this).attr("time")+"</td><td id=\"tdsms"+$(this).attr("id")+"\">"+$(this).text()+"</td><td style=\"text-align:right\"><a class=\"fb\" href=\"javascript:editsms("+$(this).attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_edit.png\" /></a><a class=\"fb\" href=\"javascript:addfiltnr("+$(this).attr("from")+")\"><img src=\"'.$url_prefix.'images/page_white_error.png\" /></a><a class=\"fb\" href=\"javascript:delwarn("+$(this).attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_delete.png\" /></a></td></tr>");
					});
					if($(request.responseXML).find("sms").length > 0){
						$("#smsinnerlist").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});
					}
					$(".smslist tr").hover(
						function(){$(this).addClass("stripelight");},
						function(){$(this).removeClass("stripelight");}
					);
					$("#smsloadlistindicator").css("display","none");

					$("#runningSMS").css("display","none");
					$("#banlabel").fadeIn();
					$("#bannedSMS").css("display","block");
					$("#smsinnerlist").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});

				}
			});

		}


		function showsmslist(){
			$("#bannedSMS").css("display","none");
			$("#banlabel").fadeOut();
			$("#runningSMS").css("display","block");
			$("#smsinnerlist").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});
		}

	function loadsmsolder(){
		$("#smsloadlistindicator").css("display","inline");

		request = $.ajax({
			url: "'.$url_prefix.'ajax/loadsmslist/&dir=down&lim=50&id="+SMSminID,
			processData: false,
			success: function(xml){
				if($(request.responseXML).find("sms").length > 0){
					$(request.responseXML).find("sms").each(function(){
						$("#lowbar").before("<tr><td>"+$(this).attr("time")+"</td><td id=\"tdsms"+$(this).attr("id")+"\">"+$(this).text()+"</td><td><a class=\"fb\" href=\"javascript:editsms("+$(this).attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_edit.png\" /></a><a class=\"fb\" href=\"javascript:addfiltnr("+$(this).attr("from")+")\"><img src=\"'.$url_prefix.'images/page_white_error.png\" /></a><a class=\"fb\" href=\"javascript:delwarn("+$(this).attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_delete.png\" /></a></td></tr>");
						SMSmaxID = Math.max(SMSmaxID, $(this).attr("id"));
						if($(this).attr("id") > 0){
							SMSminID = Math.min(SMSminID, $(this).attr("id"));
						}
					});
//					$("#smsinnerlist").jScrollPane({scrollbarWidth:20, scrollbarMargin:10});
					$("#smsinnerlist").jScrollPane({showArrows:true});

					$(".smslist tr").hover(
						function(){$(this).addClass("stripelight");},
						function(){$(this).removeClass("stripelight");}
					);
				}else{
					alert("No older messages available...");
				}
				$("#smsloadlistindicator").css("display","none");
			}
		});

	}

	function loadstats(){
		$("#stats").parent().css("background","url('.$url_prefix.'/images/loadinv.gif) bottom right no-repeat");
		$("#stats").load("'.$url_prefix.'/ajax/loadstats/");
		$("#stats").parent().css("background","none");
	}

	$(document).ready(function(){
		$(".listdiv").hover(
			function(){ $(this).addClass("listdiv_hover"); },
			function(){ $(this).removeClass("listdiv_hover"); }
		);
		$(".bigbox").jScrollPane({showArrows:true});
		$("#smsinnerlist").jScrollPane({showArrows:true});
		loadsmslist(0,\'down\', 100);
		setInterval("loadstats()", 5000);
	});
	';



	htmlheader($script);

	echo '

	<div id="lpart">
		<p class="bbmainaction"><a href="javascript:addfiltword()">+ add word</a></p>
		<h3>Filtered Words</h3>
		<div class="bigboxcover">
		<div id="fwords" class="bigbox">';

		$sql = mysql_query("SELECT * FROM filterwords ORDER BY fwText", $db);
		$filters_words = array();
		echo mysql_error();
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			array_push($filters_words, $row["fwText"]);
			echo '
				<div id="filtword'.$row["fwID"].'" class="listdiv">
					<a style="float:right; text-align:right; width: 20px; display:block" href="javascript:delfiltword('.$row["fwID"].')"><img src="'.$url_prefix.'images/cross.png" style="border:0px;" /></a>
					'.$row["fwText"].'
				</div>
			';
		}

		echo '
		</div></div>

		<p class="bbmainaction"><a href="javascript:addfiltnr()">+ add number</a></p>
		<h3>Filtered Numbers</h3>
		<div class="bigboxcover">
		<div id="fnumbers" class="bigbox">';


		$sql = mysql_query("SELECT * FROM filternumbers ORDER BY fnNR", $db);
		$filters_numers = array();
		echo mysql_error();
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			array_push($filters_numers, $row["fnNR"]);
			array_push($filters_numers, "+".$row["fnNR"]);
			echo '
				<div id="filtnr'.$row["fnID"].'" class="listdiv">
					<a style="float:right; text-align:right; width: 20px; display:block" href="javascript:delfiltnr('.$row["fnID"].')"><img src="'.$url_prefix.'images/cross.png" style="border:0px;" /></a>
					'.$row["fnNR"].'
				</div>
			';
		}



		$smssql = mysql_query("SELECT * FROM inbox");
		$temp = mysql_num_rows($smssql);
		$smssql = mysql_query("SELECT * FROM inbox WHERE SenderNumber NOT IN (\"".implode("\",\"", $filters_numers)."\") AND TextDecoded NOT LIKE \"%".implode("%\" AND TextDecoded NOT LIKE \"%", $filters_words)."%\" ORDER BY ID DESC");



		echo '
		</div></div>


		<h3>Statistics</h3>
		<div class="bigboxcover" style=""><div id="stats" style="margin: 3px;">
		</div></div>
	</div>
	';


	echo '
		<div id="rpart">
			<table class="smslistcover striped" style="width:100%">
				<tr style="height:30px;"><th>SMS List</th><th><img src="'.$url_prefix.'/images/load.gif" style="float:right" id="smsloadlistindicator" /></th></tr>
				<tr id="topbarx">
					<th style="width: 80px;">Day - hh:mm</th>
					<th><a href="javascript:addsms()" style="float:right">+ add message</a>Message</th>
				</tr>
				<tr><td colspan="2" style="padding:0px;">
				<div id="banlabel" style="display:none; position: absolute; top:0px; "><img src="'.$url_prefix.'images/banned.gif" /></div>
				<div id="smsinnerlist" style="height:400px; overflow:hidden">
					<table class="smslist" id="runningSMS">
					<tr id="topbar" style="display: none; height:1px; font-size:1px;">
						<th style="width: 80px;"></th>
						<th></th>
						<th style="width: 100px;"></th>
					</tr>
	';


/*
	$count = 0;
	while($row = mysql_fetch_array($smssql, MYSQL_ASSOC)){
		if($count < 30){
			$count++;
			echo '
			<tr>
				<td style="width: 85px;">'.substr($row["ReceivingDateTime"], 5, 11).'</td>
				<td id="tdsms'.$row["ID"].'">'.utf8_encode($row["TextDecoded"]).'</td>
				<td style="width: 70px;"><a class="fb" href="javascript:editsms('.$row["ID"].')"><img src="'.$url_prefix.'images/page_white_edit.png" /></a>
					<a class="fb" href="javascript:addfiltnr('.$row["SenderNumber"].')"><img src="'.$url_prefix.'images/page_white_error.png" /></a>
					<a class="fb" href="javascript:delwarn('.$row["ID"].')"><img src="'.$url_prefix.'images/page_white_delete.png" /></a>
				</td>
			</tr>
			';
		}
	}
*/

	echo '
				<tr id="lowbar" style="height:1px; font-size:1px;">
					<td style="width: 80px;"></td>
					<td></td>
					<td style="width: 70px;"></td>
				</tr>
				</table>

				<table class="smslist" id="bannedSMS" style="display:none">
					<tr id="bantopbar" style="display: none; height:1px; font-size:1px;">
						<th style="width: 80px;"></th>
						<th></th>
						<th style="width: 100px;"></th>
					</tr>

					<tr id="banlowbar" style="height:1px; font-size:1px;">
						<td style="width: 80px;"></td>
						<td></td>
						<td style="width: 70px;"></td>
					</tr>
				</table>


				</div>
			</td></tr>
			<tr id="lowbars"><th colspan="2"><a href="javascript:loadsmsolder()">Load older messages</a></th></tr>
			</table>
			<a href="javascript:loadbannedsmslist()">Banned SMS messages</a>
			|
			<a href="javascript:showsmslist()">Running SMS messages</a>
		</div>

		<div class="editbox"></div>
	';



	htmlfooter();
?>
OK;